/**
 * Created by chengming on 18/4/18.
 * version 1.0
 */
'use strict';
const request = require('request-promise');
const logger = require('../../conf/logger');
const urlTool = require('url');
const BaseAdapter = require('./BaseAdapter');
const iconv = require("iconv-lite");
const report = require('../../exception/ErrorReport');
const crypto = require('crypto')
const contentReport = require('../../exception/ContentReport');

class BaseHrefAdapter extends BaseAdapter {

	constructor(page, isTest) {
		super(page, page.encoding||"UTF-8", isTest, true);

		this.pageSite = page.pageSite;
		this.pageTitle = page.pageTitle;
		this.channel = page.channel;
		this.cates = page.cates;
		this.foreign = page.foreign;
		this.contentRule = page.contentRule;
		this.linkRule = page.linkRule;
		this.titleRule = page.titleRule;
		
		this.linkUniqueBy = page.linkUniqueBy ? page.linkUniqueBy : 1;
		this.followLink = page.followLink;
		this.sourceTime = page.createtime;
		this.id = page.id;
		this.rangeMd5 = page.rangeMd5;
	}

	parseHtml(html, linkRule){

	}

	async execute(){
		logger.debug('开始执行抓取->'+this.pageName);
		/*if(this.pageSite == 'https://ambcrypto.com/category/news/') {
            await report(this.pageSite, this.pageTitle, '', '开始请求');
		}*/
		//获取请求结果
		let body = await this.doRequest();

		//根据指定encoding解析页面
		let decodeBody = iconv.decode(body, this.encoding).toString();

    	await contentReport(this.cates, this.pageTitle, this.channel, decodeBody);

        /*if(this.pageSite == 'https://ambcrypto.com/category/news/') {
            let startIndex = decodeBody.indexOf('<div class="mvp-blog-story-text left relative"');
            await report(this.pageSite, this.pageTitle, '', '请求结束,decodeBody: ' + decodeBody.substr(startIndex));
        }*/
		logger.debug(this.pageName+'抓取执行完毕,开始分析文本');

		if(decodeBody.trim().length<5) {
            logger.warn(this.pageName+'抓取到的页面为空');
            if(this.isTest == false){
                await report(this.pageSite, this.pageTitle, '', '抓取到的页面为空');
            }
            return {
                code: 401,
                errMsg: '抓取到的页面为空'
            };
		}

		if(!this.linkRule || this.linkRule.length == 0){
			logger.warn(this.pageName+'没有链接抓取规则');
			if(this.isTest == false){
				await report(this.pageSite, this.pageTitle, '', '没有链接抓取规则');
			}
			return {
				code: 401,
				errMsg: '没有链接抓取规则'
			};
		}

		let contentBlock = this.parseHtml(decodeBody, this.linkRule);
		if(contentBlock.rangeText.length == 0) {
			logger.warn(this.pageName + ">>>" + this.linkRule + "此抓取规则下,无内容匹配，decodeBody：" + decodeBody);
			if (this.isTest == false) {
				await report(this.pageSite, this.pageTitle, '', this.linkRule + "此抓取规则下,无内容匹配, decodeBody: " + decodeBody);
			}
			return {
				code: 402,
				errMsg: this.linkRule + "此抓取规则下,无内容匹配" + decodeBody
			};
		}

        await super.postSocket(this.id, this.rangeMd5, contentBlock.rangeText, contentBlock.contents,  decodeBody, this.cates, this.pageTitle, this.sourceTime, this.channel,
			this.followLink, this.foreign, this.contentRule, this.linkUniqueBy);

		return await super.postFilterUnique(contentBlock.contents,
				this.cates,
				this.foreign,
				this.linkUniqueBy,
				this.followLink,
				this.sourceTime,
				this.pageTitle,
				this.titleRule,
				this.contentRule,
				this.channel);
	}

}

module.exports = BaseHrefAdapter;